package com.sdtv.basemodule.base;

import android.app.Application;
import android.content.Context;

import androidx.multidex.MultiDex;

import com.blankj.utilcode.util.Utils;
import com.scwang.smartrefresh.header.MaterialHeader;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.footer.ClassicsFooter;
import com.sdtv.basemodule.config.AppConfig;
import com.sdtv.basemodule.config.BaseConfig;
import com.sdtv.basemodule.router.ARouterUtils;

/**
 * @ProjectName: MediunFusion
 * @Package: com.sdtv.basemodule.base
 * @ClassName: BaseApplication
 * @Description: application全局环境
 * @Author: 小郭同学
 * @CreateDate: 2019-06-19 13:40
 * @UpdateRemark: 更新说明
 * @Version: 1.0
 */
public abstract class BaseApplication<S extends BaseConfig> extends Application {

    private static Context context;

    protected static BaseConfig config;

    public static BaseConfig getConfig() {
        return config;
    }

    /**
     * 该方法是执行在onCreate之前被调用
     *
     * @param base
     */
    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
        MultiDex.install(this);
    }

    public static Context getContext() {
        return context;
    }


    static {
        //设置全局的Header构建器
        SmartRefreshLayout.setDefaultRefreshHeaderCreator((context, layout) -> {
            MaterialHeader materialHeader = new MaterialHeader(context);
            materialHeader.setColorSchemeColors(0xff333333, 0xffff4444, 0xff669900, 0xffaa66cc, 0xffff8800);
            return materialHeader;
        });
        //     SmartRefreshLayout.setDefaultRefreshHeaderCreator((context, layout) -> new ClassicsHeader(application));
        SmartRefreshLayout.setDefaultRefreshFooterCreator((context, layout) -> new ClassicsFooter(context));
    }


    @Override
    public void onCreate() {
        super.onCreate();
        context = this;
        Utils.init(this);
        initConfigure();
        if (null == config) {
            throw new IllegalStateException("Configure is not init");
        }
        AppConfig.INSTANCE.initConfig(config);
        init();
    }

    /**
     * 初始化app相关配置
     */
    protected abstract void initConfigure();

    /**
     * 初始化一些操作
     */
    protected abstract void init();


    /**
     *
     */
    @Override
    public void onTerminate() {
        super.onTerminate();
        //销毁路由--释放资源
        ARouterUtils.destroy();
    }


}
